package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.system.domain.TDoctor;
import com.ruoyi.system.domain.TPatient;
import com.ruoyi.system.domain.vo.TMedicalOrderVo;
import com.ruoyi.system.service.ISysPostService;
import com.ruoyi.system.service.ITDoctorService;
import com.ruoyi.system.service.ITPatientService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TMedicalOrder;
import com.ruoyi.system.service.ITMedicalOrderService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 医嘱信息Controller
 * 
 * @author 杨一迪
 * @date 2024-12-29
 */
@Controller
@RequestMapping("/system/order")
public class TMedicalOrderController extends BaseController
{
    private String prefix = "system/order";

    @Autowired
    private ITMedicalOrderService tMedicalOrderService;

    @Resource
    private ITDoctorService doctorService;

    @Resource
    private ITPatientService patientService;

    @RequiresPermissions("system:order:view")
    @GetMapping()
    public String order(ModelMap mmap)
    {

        return prefix + "/order";
    }

    /**
     * 查询医嘱信息列表
     */
    @RequiresPermissions("system:order:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TMedicalOrder tMedicalOrder)
    {
        startPage();
        List<TMedicalOrder> list = tMedicalOrderService.selectTMedicalOrderList(tMedicalOrder);
        List<TMedicalOrderVo> medicalOrderVos = new ArrayList<>();
        list.forEach(medical -> {
            TMedicalOrderVo tMedicalOrderVo = new TMedicalOrderVo();
            BeanUtils.copyProperties(medical,tMedicalOrderVo);
            TDoctor tDoctor = doctorService.selectTDoctorById(medical.getDoctorId());
            TPatient tPatient = patientService.selectTPatientById(medical.getPatientId());
            tMedicalOrderVo.setDoctorName(tDoctor.getName());
            tMedicalOrderVo.setPatientName(tPatient.getName());
            medicalOrderVos.add(tMedicalOrderVo);
        });
        return getDataTable(medicalOrderVos);
    }

    /**
     * 导出医嘱信息列表
     */
    @RequiresPermissions("system:order:export")
    @Log(title = "医嘱信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TMedicalOrder tMedicalOrder)
    {
        List<TMedicalOrder> list = tMedicalOrderService.selectTMedicalOrderList(tMedicalOrder);
        ExcelUtil<TMedicalOrder> util = new ExcelUtil<TMedicalOrder>(TMedicalOrder.class);
        return util.exportExcel(list, "医嘱信息数据");
    }

    /**
     * 新增医嘱信息
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        List<TPatient> tPatients = patientService.selectTPatientList(new TPatient());
        List<TDoctor> tDoctors = doctorService.selectTDoctorList(new TDoctor());
        mmap.put("tPatients",tPatients);
        mmap.put("tDoctors",tDoctors);
        return prefix + "/add";
    }

    /**
     * 新增保存医嘱信息
     */
    @RequiresPermissions("system:order:add")
    @Log(title = "医嘱信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TMedicalOrder tMedicalOrder)
    {
        return toAjax(tMedicalOrderService.insertTMedicalOrder(tMedicalOrder));
    }

    /**
     * 修改医嘱信息
     */
    @RequiresPermissions("system:order:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TMedicalOrder tMedicalOrder = tMedicalOrderService.selectTMedicalOrderById(id);
        mmap.put("tMedicalOrder", tMedicalOrder);
        List<TPatient> tPatients = patientService.selectTPatientList(new TPatient());
        List<TDoctor> tDoctors = doctorService.selectTDoctorList(new TDoctor());
        mmap.put("tPatients",tPatients);
        mmap.put("tDoctors",tDoctors);
        return prefix + "/edit";
    }

    /**
     * 修改保存医嘱信息
     */
    @RequiresPermissions("system:order:edit")
    @Log(title = "医嘱信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TMedicalOrder tMedicalOrder)
    {
        return toAjax(tMedicalOrderService.updateTMedicalOrder(tMedicalOrder));
    }

    /**
     * 删除医嘱信息
     */
    @RequiresPermissions("system:order:remove")
    @Log(title = "医嘱信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tMedicalOrderService.softDeleteTMedicalOrderByIds(ids));
    }
}
